home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / DrawTest.java < prev    next >
Text File  |  1998-09-15  |  7KB  |  255 lines

  1. /*
  2.  * @(#)DrawTest.java    1.4 98/03/23
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.event.*;
  32. import java.awt.*;
  33. import java.applet.*;
  34.  
  35. import java.util.Vector;
  36.  
  37. public class DrawTest extends Applet{
  38.     DrawPanel panel;
  39.     DrawControls controls;
  40.  
  41.     public void init() {
  42.     setLayout(new BorderLayout());
  43.     panel = new DrawPanel();
  44.         controls = new DrawControls(panel);
  45.     add("Center", panel);
  46.     add("South",controls);
  47.     }
  48.     
  49.     public void destroy() {
  50.         remove(panel);
  51.         remove(controls);
  52.     }
  53.  
  54.     public static void main(String args[]) {
  55.     Frame f = new Frame("DrawTest");
  56.     DrawTest drawTest = new DrawTest();
  57.     drawTest.init();
  58.     drawTest.start();
  59.  
  60.     f.add("Center", drawTest);
  61.     f.setSize(300, 300);
  62.     f.show();
  63.     }
  64.     public String getAppletInfo() {
  65.         return "A simple drawing program.";
  66.     }
  67. }
  68.  
  69. class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
  70.     public static final int LINES = 0;
  71.     public static final int POINTS = 1;
  72.     int       mode = LINES;
  73.     Vector lines = new Vector();
  74.     Vector colors = new Vector();
  75.     int x1,y1;
  76.     int x2,y2;
  77.     int xl, yl;
  78.  
  79.     public DrawPanel() {
  80.     setBackground(Color.white);
  81.     addMouseMotionListener(this);
  82.     addMouseListener(this);
  83.     }
  84.  
  85.     public void setDrawMode(int mode) {
  86.     switch (mode) {
  87.       case LINES:
  88.       case POINTS:
  89.         this.mode = mode;
  90.         break;
  91.       default:
  92.         throw new IllegalArgumentException();
  93.     }
  94.     }
  95.  
  96.  
  97.     public void mouseDragged(MouseEvent e) {
  98.         e.consume();
  99.         switch (mode) {
  100.             case LINES:
  101.                 xl = x2;
  102.                 yl = y2;
  103.                 x2 = e.getX();
  104.                 y2 = e.getY();
  105.                 break;
  106.             case POINTS:
  107.             default:
  108.                 colors.addElement(getForeground());
  109.                 lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
  110.                 x1 = e.getX();
  111.                 y1 = e.getY();
  112.                 break;
  113.         }
  114.         repaint();
  115.     }
  116.  
  117.     public void mouseMoved(MouseEvent e) {
  118.     }
  119.  
  120.     public void mousePressed(MouseEvent e) {
  121.         e.consume();
  122.         switch (mode) {
  123.             case LINES:
  124.                 x1 = e.getX();
  125.                 y1 = e.getY();
  126.                 x2 = -1;
  127.                 break;
  128.             case POINTS:
  129.             default:
  130.                 colors.addElement(getForeground());
  131.                 lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
  132.                 x1 = e.getX();
  133.                 y1 = e.getY();
  134.                 repaint();
  135.                 break;
  136.         }
  137.     }
  138.  
  139.     public void mouseReleased(MouseEvent e) {
  140.         e.consume();
  141.         switch (mode) {
  142.             case LINES:
  143.                 colors.addElement(getForeground());
  144.                 lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
  145.                 x2 = xl = -1;
  146.                 break;
  147.             case POINTS:
  148.             default:
  149.                 break;
  150.         }
  151.         repaint();  
  152.     }
  153.  
  154.     public void mouseEntered(MouseEvent e) {
  155.     }
  156.  
  157.     public void mouseExited(MouseEvent e) {
  158.     }
  159.  
  160.     public void mouseClicked(MouseEvent e) {
  161.     }
  162.  
  163.     public void paint(Graphics g) {
  164.     int np = lines.size();
  165.  
  166.     /* draw the current lines */
  167.     g.setColor(getForeground());
  168.     g.setPaintMode();
  169.     for (int i=0; i < np; i++) {
  170.         Rectangle p = (Rectangle)lines.elementAt(i);
  171.         g.setColor((Color)colors.elementAt(i));
  172.         if (p.width != -1) {
  173.         g.drawLine(p.x, p.y, p.width, p.height);
  174.         } else {
  175.         g.drawLine(p.x, p.y, p.x, p.y);
  176.         }
  177.     }
  178.     if (mode == LINES) {
  179.         g.setXORMode(getBackground());
  180.         if (xl != -1) {
  181.         /* erase the last line. */
  182.         g.drawLine(x1, y1, xl, yl);
  183.         }
  184.         g.setColor(getForeground());
  185.         g.setPaintMode();
  186.         if (x2 != -1) {
  187.         g.drawLine(x1, y1, x2, y2);
  188.         }
  189.     }
  190.     }
  191. }
  192.  
  193.  
  194. class DrawControls extends Panel implements ItemListener {
  195.     DrawPanel target;
  196.  
  197.     public DrawControls(DrawPanel target) {
  198.     this.target = target;
  199.     setLayout(new FlowLayout());
  200.     setBackground(Color.lightGray);
  201.     target.setForeground(Color.red);
  202.     CheckboxGroup group = new CheckboxGroup();
  203.     Checkbox b;
  204.     add(b = new Checkbox(null, group, false));
  205.     b.addItemListener(this);
  206.     b.setBackground(Color.red);
  207.     add(b = new Checkbox(null, group, false));
  208.     b.addItemListener(this);
  209.     b.setBackground(Color.green);
  210.     add(b = new Checkbox(null, group, false));
  211.     b.addItemListener(this);
  212.     b.setBackground(Color.blue);
  213.     add(b = new Checkbox(null, group, false));
  214.     b.addItemListener(this);
  215.     b.setBackground(Color.pink);
  216.     add(b = new Checkbox(null, group, false));
  217.     b.addItemListener(this);
  218.     b.setBackground(Color.orange);
  219.     add(b = new Checkbox(null, group, true));
  220.     b.addItemListener(this);
  221.     b.setBackground(Color.black);
  222.     target.setForeground(b.getForeground());
  223.     Choice shapes = new Choice();
  224.     shapes.addItemListener(this);
  225.     shapes.addItem("Lines");
  226.     shapes.addItem("Points");
  227.     shapes.setBackground(Color.lightGray);
  228.     add(shapes);
  229.     }
  230.  
  231.     public void paint(Graphics g) {
  232.     Rectangle r = getBounds();
  233.  
  234.     g.setColor(Color.lightGray);
  235.     g.draw3DRect(0, 0, r.width, r.height, false);
  236.     }
  237.  
  238.   public void itemStateChanged(ItemEvent e) {
  239.     if (e.getSource() instanceof Checkbox) {
  240.       target.setForeground(((Component)e.getSource()).getBackground());
  241.     } else if (e.getSource() instanceof Choice) {
  242.       String choice = (String) e.getItem();
  243.       if (choice.equals("Lines")) {
  244.     target.setDrawMode(DrawPanel.LINES);
  245.       } else if (choice.equals("Points")) {
  246.     target.setDrawMode(DrawPanel.POINTS);
  247.       }
  248.     }
  249.   }
  250. }
  251.     
  252.  
  253.  
  254.     
  255.